package org.softwarepoets.emf.ecore.editor.extensions;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.actions.ActionDelegate;

public abstract class AbstractExtensionAction extends ActionDelegate implements
		IActionDelegate {
	
	protected Object element;
	
	protected Class<? extends EObject> target;
	
	protected AbstractExtensionAction(Class<? extends EObject> target) {
		this.target = target;
	}
	
	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object object = ((IStructuredSelection) selection)
					.getFirstElement();
			if (target.isInstance(object)) {
				element = object;

				action.setEnabled(true);
				return;
			}
		}
		element = null;
		action.setEnabled(false);
	}

}
